package vikaa.adapter;


import java.util.ArrayList;

import vikaa.bean.Space;
import vikaa.timelinepackage.AsyLoadImg;
import vikaa.tools.HttpUtil;
import vikaa.tools.StringUtils;
import vikaa.wowo.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MySpaceListAdapter extends BaseAdapter{
    private Context context;
	private LayoutInflater inflater;
	private final ArrayList<Space>  wbList; 
    
    public MySpaceListAdapter(Context context, ArrayList<Space> wbList) {
		super();
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.wbList = wbList;
	}

	public int getCount() {
        return wbList.size();
    }

    public Object getItem(int position) {
        return wbList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    	Holder wh;
    	if(convertView == null){
	        convertView = inflater.inflate(R.layout.mywowo_item, null);
	        wh = new Holder();
	        wh.wowo_cover = (ImageView) convertView.findViewById(R.id.wowo_cover);
	        wh.wowo_name = (TextView) convertView.findViewById(R.id.wowo_name);
	        wh.wowo_lock = (ImageView) convertView.findViewById(R.id.wowo_lock);
	        wh.wowo_mems2 = (TextView) convertView.findViewById(R.id.wowo_mems1);
	        wh.wowo_dev2 = (TextView) convertView.findViewById(R.id.wowo_dev1);
	        wh.wowo_msgNum = (TextView)convertView.findViewById(R.id.wowo_msgnumTv);
	        convertView.setTag(wh);
    	}
    	else{
    		wh = (Holder)convertView.getTag();
    	}
        Space wb = wbList.get(position);
        wh.wowo_name.setText(wb.getSpacename());
        if(wb.getSpacetype().equals("mySpace")){
	        wh.wowo_mems2.setText("成员:"+wb.getTotalMemberCount());
	        wh.wowo_dev2.setText("发展度:"+wb.getCount());
	        if(Integer.valueOf(wb.getNewMsg()) == 0 || wb.isCheckMsg()){
		        wh.wowo_msgNum.setVisibility(View.INVISIBLE);
	        }else{
	        	wh.wowo_msgNum.setVisibility(View.VISIBLE);
	        	wh.wowo_msgNum.setText(wb.getNewMsg());
		        wh.wowo_msgNum.setBackgroundResource(R.drawable.wowo_item_num_bg);
	        }
	        }
        if(wb.getSpacetype().equals("joinedSpace")){
	        wh.wowo_mems2.setText("成员:"+wb.getTotalMemberCount());
	        wh.wowo_dev2.setText("发展度:"+wb.getCount());
	        if(Integer.valueOf(wb.getNewMsg()) == 0){
		        wh.wowo_msgNum.setVisibility(View.INVISIBLE);
	        }else{
	        	wh.wowo_msgNum.setVisibility(View.VISIBLE);
	        	wh.wowo_msgNum.setText(wb.getNewMsg());
		        wh.wowo_msgNum.setBackgroundResource(R.drawable.wowo_item_num_bg);
	        }
	    }
        //Logger.i(wb.getState()+"  "+position);
        if(!StringUtils.isEmpty(wb.getState())){
	        if(wb.getState().equals("1") ){
	        	wh.wowo_lock.setVisibility(View.VISIBLE);
	        	wh.wowo_mems2.setText(wb.getCount() + "个瞬间");
		        wh.wowo_dev2.setText("");
	        }else{
	        	wh.wowo_lock.setVisibility(View.GONE);
	        }
        }
        
        if(wb.getSpacetype().equals("invitedSpace")){
	        wh.wowo_mems2.setText(wb.getSpaceoauth().get(0).getNickname_fromuser() +"正在邀请您加入...");
	        wh.wowo_dev2.setText("");
	        wh.wowo_msgNum.setVisibility(View.INVISIBLE);
	    }
        
        if(!StringUtils.isEmpty(wb.getCover())){
            AsyLoadImg.loadAvater(HttpUtil.HttpUrlComplete(wb.getCover()) , wh.wowo_cover);
        }
        else{
        	wh.wowo_cover.setImageResource(R.drawable.wowo_item_cover);
        }
        return convertView;
    }
    static class Holder {
    	public ImageView wowo_cover = null;
		public ImageView wowo_lock = null;
		public TextView wowo_name = null;
		public TextView wowo_mems2 = null;
		public TextView wowo_dev2 = null;
		public TextView wowo_msgNum = null;
    }
}

